import axios from 'axios'
import NProgress from 'nprogress'
import router from "@/router"
import store from "@/store";

import {useRoute} from "vue-router";
import { getToken } from "@/utils/jsCookie";
import { addPending, removePending } from "@/utils/requestCancel"

import 'nprogress/nprogress.css'
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })


// 不需要认证
const whiteList = ['/admin/login']
export const pendingRequests = new Map()

const instance = axios.create({
    baseURL: process.env.NODE_ENV === 'development'? '/api-sit': '/prod-api',
    // baseURL:'/',// process.env.NODE_ENV === 'development'? '/api-lwj': '/prod-api',‘’
    headers: {'Content-Type': 'application/json;charset=UTF-8'},
    timeout: 25 * 1000
})

/**
 * @description 请求拦截器
 */
instance.interceptors.request.use(
    (config) => {
        config.params = config.params && Object.fromEntries(Object.entries(config.params).filter((o) => typeof o[1] === 'number' && !isNaN(o[1])? true: o[1]));

        NProgress.start()
    
        removePending(config);
        addPending(config);
        
        if(!whiteList.includes(config.url) && getToken()) {
            config.headers['Authorization'] = getToken();
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

/**
 * @description 响应拦截器
 */
instance.interceptors.response.use(
    (response) => {
        NProgress.done();
        
        let { data, code, config, headers } = response
        removePending(config);
        
        // 有 x-total-count 属性表示是模拟数据
        if(headers['x-total-count']) {
            data = {
                code: 20000,
                data: {
                    list: data,
                    pageNum: config.params._page,
                    pageSize: config.params._limit,
                    total: Number(headers['x-total-count'])
                },
                msg: "查询成功"
            };
        }
        
        switch (code || data.code) {
            case 40003:
            case 40010:
            case 40015:
                store.dispatch("user/clearUserInfo");
                break;
            default:
                break
        }
        return data
    },
    (error) => {
        NProgress.done();
    
        if(axios.isCancel(error)) {
            return new Promise(()=> {})
        } else {
            const { response } = error;
            
            if(response?.status === 403) {
                store.dispatch("user/clearUserInfo").then(() => {
                    const route = useRoute();
                    router.push(`/login?redirect=${ route.path }`)
                })
            }
            
            return Promise.reject(error)
        }
    }
)

export default instance